<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'回收规则列表'),
	array('op'=>'edit','title'=>'添加客户回收规则'),
);
//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_member_recykey',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['keys']=$item['keys']?json_decode($item['keys'],true):array();
			$item['keystitle']='进入'.$this->user_buystatus[$item['keys'][0]['user_buystatus']].','.$item['keys'][0]['days'].'天未跟进';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：当通用规则（指所有员工）和特殊化规则（指个人规则）同时存在的情况下，员工则执行某执行特殊化规则。比如，张三既在通用规则，又在特殊化规则，那么，就按特殊化规则执行。
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'bumenid','title'=>'生效成员','type'=>'select','options'=>'groups'),
			array('name'=>'keystitle','title'=>'回收规则','type'=>'string'),
			array('name'=>'membernum','title'=>'成员限制','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),	
	);
	$return['groups']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member_recykey',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['keys']=$item['keys']?json_decode($item['keys'],true):array(
		array('user_buystatus'=>0,'days'=>3,'noticedays'=>1)
	);
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回规则列表'),
		),
		'edit'=>array(
					array('type'=>'select','title'=>'生效成员','name'=>'bumenid','options'=>'group','help'=>'针对不同员工配置不同的客户回收规则'),
					array(
							'type'=>'array',
							'name'=>'keys',
							'title'=>'规则',
							'add_tip'=>'添加一个规则',
								'list'=>array(
									array('type'=>'radio','name'=>'user_buystatus','title'=>'客户状态','options'=>'user_buystatus'),
									array('type'=>'string','name'=>'days','title'=>'多少天未添加记录'),
									array('type'=>'string','name'=>'noticedays','title'=>'在回收前N天提醒'),
								),
								'details'=>array(
									array('type'=>'radio','name'=>'user_buystatus','title'=>'客户状态','options'=>'user_buystatus'),
									array('type'=>'string','name'=>'days','title'=>'多少天未添加记录'),
									array('type'=>'string','name'=>'noticedays','title'=>'在回收前N天提醒'),
								)
						),
					array('type'=>'string','title'=>'非企微客户数量','name'=>'membernum','help'=>'每个员工个人所持有的非企微客户上限数量（在【客户管理】-【非企微客户】列表，员工录入/批量导入客户数量不计入在其中。）'),
					array('type'=>'switch','title'=>'允许延期','name'=>'open_yanqi','show'=>array('open_yanqi'=>1)),
					array('type'=>'unitstring','title'=>'允许员工延期','unit'=>'天','name'=>'yanqi_days','help'=>''),
					array('type'=>'unitstring','title'=>'员工捡回','unit'=>'天','name'=>'getback_days','help'=>'N天后可重新捡回（在某员工手上丢失的客户，回收至公海池，只可在规定的时间后才可重新认领）'),
		)
	);
	$return['group']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['user_buystatus']=$this->user_buystatus;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'bumenid'=>intval($postdata['bumenid']),
		'membernum'=>intval($postdata['membernum']),
		'open_yanqi'=>intval($postdata['open_yanqi']),
		'yanqi_days'=>intval($postdata['yanqi_days']),
		'getback_days'=>intval($postdata['getback_days']),
		'keys'=>ijson_encode($postdata['keys']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_member_recykey',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_member_recykey',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member_recykey',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_member',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='upmember'){
	$userid='';
	$this->model('member')->batch();
}